<?php
// +----------------------------------------------------------------------
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +----------------------------------------------------------------------

namespace app\user\controller;
use app\common\controller\Front;
use PHPMailer\PHPMailer\PHPMailer;
use think\Db;
use think\Loader;
use think\Session;


class Login extends Front{

	public function index($username = '', $password = '', $verify = ''){
		if (IS_POST) {
			if (!$username || !$password) {
				return $this->error('用户名或者密码不能为空！','');
			}
			//验证码验证
			$this->checkVerify($verify);
			$user = model('User');
			$uid = $user->login($username,$password);
			if ($uid > 0) {
				$url = session('http_referer') ? session('http_referer') : url('user/index/index');
				return $this->success('登录成功！', $url);
			}else{
				switch($uid) {
					case -1: $error = '用户不存在或被禁用！'; break; //系统级别禁用
					case -2: $error = '密码错误！'; break;
					default: $error = '未知错误！'; break; // 0-接口参数错误（调试阶段使用）
				}
				return $this->error($error,'');
			}
		}else{
			session('http_referer', isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '');
			if (is_login()) {
				return $this->redirect('user/index/index');
			}else{
				return $this->fetch();
			}
		}
	}

	public function logout(){
		//model('User')->logout();
        session('org_id', null);
        session('name', null);
        return $this->redirect('index/index/index');
	}

	public function update_org_pwd(){
        $phone = $_POST['phone'];
        $code = $_POST['code'];
        $password = $_POST['password'];

        if($code != Session::get('code')){
            $req_data['code'] = 100;//验证码错误
            echo json_encode($req_data);
            die(0);
        }
        $data = ['password'=>$password];

        $orinfo = Db::table('organization')->where('phone',$phone)->update($data);
        $req_data['code'] = 200;//验证码错误
        echo json_encode($req_data);
    }

	public function add_organization(){

        Loader::import('phpqrcode.phpqrcode');

        $phone = $_POST['phone'];
        $name = $_POST['name'];
        $code = $_POST['code'];
        $password = $_POST['password'];
        $req_data = array();

        if($code != Session::get('code')){
            $req_data['code'] = 100;//验证码错误
            echo json_encode($req_data);
            die(0);
        }
        $data = ['name' => $name, 'phone' => $phone,'password'=>$password];
         Db::table('organization')->insert($data);
        $orinfo = Db::table('organization')->where('phone',$phone)->find();
        $id = $orinfo['id'];
        session('org_id', $id);
        session('name', $name);


         $item_type = 'qrcode';
       // $logo = ROOT_PATH."static/css/default/img/logo.png";
        $qr_data = array();
        $qr_data['name'] = $name;
        $qr_data['id'] = $id;

        $data = json_encode($qr_data);

        $errorCorrectionLevel = 'H';//容错级别
        $matrixPointSize = 12;//生成图片大小


        $save_dir =  '/picture' ;
        $upload_path = ROOT_PATH.'uploads'.$save_dir;
        if (!file_exists($upload_path)){
            mkdir($upload_path);
        }
        $name = $id.time().rand(1,10000000).".png";
        $QR =$upload_path.'/'.$name;//已经生成的原始二维码图
        //$delete_file = $QR;
        $db_dir = 'picture/'.$name;


        \QRcode::png($data, $QR, $errorCorrectionLevel, $matrixPointSize, 2);

        Db::table('organization')->where('id',$id)->update(['scan_code' => $db_dir]);

        $req_data['code'] = 200;//验证码错误
        echo json_encode($req_data);

    }


	public function company_data(){

        $org_id = Session::get('org_id');
        if(!$org_id){
            $this->redirect('/register');
        }
        return $this->fetch();

    }

    public function upload_cert(){
        $file = request()->file('image');
        // 验证图片,并移动图片到框架目录下。
        $req_data = array();

        $info = $file -> validate(['size' => 1024000000,'ext' => 'jpg,png,jpeg','type' => 'image/jpeg,image/png']) -> move(ROOT_PATH.'uploads'.DS.'cert');
        if($info){
           // $mes= $info->getExtension();
            $mes =  $info->getSaveName();

            //$mes = $info->getFilename();      // 文件名
            $req_data['filename'] = $mes;
            $req_data['code'] = 100;
        }else{
            // 文件上传失败后的错误信息
            $mes = $file->getError();
            $req_data['code'] = 100;
        }
        echo json_encode($req_data);


    }

    public function update_org(){

        $city = $_POST['city'];
        $province = $_POST['province'];
        $url = $_POST['url'];
        $org_id = Session::get('org_id');
        $req_data = array();
        if(!$org_id){
            $req_data['code'] = 100;
            echo json_encode($req_data);
            die(0);
        }
        error_log("====update_org====org_id===".$org_id."======city=====".$city."======province======".$province);
        Db::table('organization')->where('id', $org_id)->update(['city' => $city,'province'=>$province,'cert_photo'=>$url]);
        $req_data['code'] = 200;
        echo json_encode($req_data);

    }


	public function register($username = '', $password = '', $repassword = '', $email = '', $verify = ''){
		if (IS_POST) {
			$user = model('User');
			
			//验证码验证
			$this->checkVerify($verify);

			if ($username == '' || $password == '' || $repassword == '') {
				return $this->error("请填写完整注册信息！", '');
			}
			$result = $user->register($username, $password, $repassword, $email);
			if ($result) {
				return $this->success('注册成功！', url('user/index/index'));
			}else{
				return $this->error($user->getError(), '');
			}
		}else{
			if (is_login()) {
				$this->redirect('user/index/index');
			}
			return $this->fetch();
		}
	}





	public function forget($email = '', $verify = ''){
		if (IS_POST) {
			//验证码验证
			$this->checkVerify($verify);
			if (!$email) {
				return $this->error('邮件必填！', url('index/index/index'));
			}
			$result = false;
			$user = db('Member')->where(array('email'=>$email))->find();
			if (!empty($user)){
				$time = time();
				$token = authcode($user['uid'] . "\n\r" . $user['email'] . "\n\r" . $time, 'ENCODE');
				config('url_common_param', true);
				$url = url('user/login/find',array('time'=>$time, 'token'=>$token), 'html', true);
				$html = \think\Lang::get('find_password', array('url'=>$url));

				$result = send_email($user['email'], '找回密码确认邮件', $html);
			}
			if ($result) {
				return $this->success("已发送邮件至您邮箱，请登录您的邮箱！", url('index/index/index'));
			}else{
				return $this->error('发送失败！', '');
			}
		}else{
			return $this->fetch();
		}
	}

	public function find(){
		//http://127.0.0.2/user/login/find.html?time=1467174578&token=b561PJhVI2OjWUPNLsAMdeW8AKZLw/RcqyXUHBa1mCiX2OUzvq0D69Rt40F/n7zfJKR05d7qA41G6/33NQ
		if (IS_POST) {
			$data = $this->request->post();
			//验证码验证
			$this->checkVerify($data['verify']);
			if ($data['password'] !== $data['repassword']) {
				return $this->error('确认密码和密码不同！','');
			}

			$token_decode = authcode($data['token']);
			list($uid, $email, $time) = explode("\n\r", $token_decode);
			
			$save['salt'] = rand_string(6);
			$save['password'] = md5($data['password'].$save['salt']);
			$result = db('Member')->where(array('uid'=>$uid))->update($save);
			if (false != $result) {
				return $this->success('重置成功！');
			}else{
				return $this->success('重置失败！');
			}
		}else{
			$time = input('get.time', '', 'trim');
			$token = input('get.token', '', 'trim');
			if (!$time || !$token) {
				return $this->error('参数错误！','');
			}

			$token_decode = authcode($token);
			list($uid, $email, $time) = explode("\n\r", $token_decode);

			if ((time() - $time) > 3600 || (time() - $time) < 0) {
				return $this->error('链接已失效！', '');
			}
			if ($time != $time) {
				return $this->error('非法操作！', '');
			}

			$data = array(
				'token'  => $token,
				'email'  => $email,
				'uid'  => $uid,
			);
			$this->assign($data);
			return $this->fetch();
		}
	}

	public function org_admin(){

	   return  $this->view->fetch("user_admin/index");
    }

    public  function update_join_member(){

        $id = $_GET['id'];
        $is_add = 0;
        $user = Db::table('tmp_users')->where('id', $id)->find();
        $this->assign('user',$user);
        $this->assign('add',$is_add);
        $org_id = Session::get('org_id');
        $list =  Db::table('dept')->where('org_id',$org_id)->select();
        $this->assign('list', $list);

        return  $this->view->fetch("user_admin/add_join_member");
    }


    public function update_dept(){

        $id = $_GET['id'];
        $is_add = 0;
        $dept = Db::table('dept')->where('id', $id)->find();
        $this->assign('dept',$dept);
        $this->assign('add',$is_add);
        return  $this->view->fetch("user_admin/add_dept");

    }

    public function ajax_delete_dept(){

        $id = $_POST['id'];
        Db::table('dept')->where('id', $id)->delete();
        $req_data['code'] = 200;
        echo json_encode($req_data);
    }

    public function ajax_delete_user(){
        $id = $_POST['id'];
        Db::table('users')->where('id', $id)->delete();
        $req_data['code'] = 200;
        echo json_encode($req_data);
    }

    public function add_dept(){
        $is_add = 1;
        $this->assign('add',$is_add);
        return  $this->view->fetch("user_admin/add_dept");

    }


    public function update_job(){

        $org_id = Session::get('org_id');
        $job = $_POST['job'];
        Db::table('organization')->where('id', $org_id)->update(['job' => $job]);
        $req_data = array();
        $req_data['code'] = 200;
        echo json_encode($req_data);
    }

    public function  ajax_add_dept(){
        $org_id = Session::get('org_id');

        $name = $_POST['name'];
        $id = $_POST['id'];
        if($id != -1){
            Db::table('dept')->where('id', $id)->update(['name' => $name]);
        }else{

            $data = ['name' => $name,'org_id'=>$org_id];
            Db::table('dept')->insert($data);
        }
        $req_data = array();
        $req_data['code'] = 200;
        echo json_encode($req_data);

    }

    public function ajax_add_join_member(){

        $org_id = Session::get('org_id');

        $name = $_POST['name'];
        $email = $_POST['email'];
        $phone = $_POST['phone'];
        $dept = $_POST['dept'];
        $id = $_POST['uid'];
        if($id != -1){
            Db::table('tmp_users')->where('id', $id)->update(['name' => $name,'org_id'=>$org_id,'phone'=>$phone,'email'=>$email,'dept_name'=>$dept]);
        }else{

            $data = ['name' => $name,'org_id'=>$org_id,'phone'=>$phone,'email'=>$email,'dept_name'=>$dept];
            Db::table('tmp_users')->insert($data);
        }


        $req_data = array();
        $req_data['code'] = 200;
        echo json_encode($req_data);
    }

    public function login_by_phone(){

        $phone = $_POST['phone'];
        $code = $_POST['code'];
        $req_data = array();
//        if($code != Session::get('code')){
//            $req_data['code'] = 100;
//            echo json_encode($req_data);
//            die(0);
//        }

        $org = Db::table('organization')->where('phone', $phone)->where('password',$code)->find();
        if($org){
            session('org_id', $org['id']);
            session('name', $org['name']);

            $req_data['code'] = 200;
        }else{
            $req_data['code'] = 100;
        }

        echo json_encode($req_data);


    }

    public function dept_list(){
        $org_id = Session::get('org_id');
        if(!$org_id){
            $this->redirect('/register');
        }
        $list =  Db::table('dept')->where('org_id',$org_id)->select();
        $this->assign('list', $list);
        return  $this->view->fetch("user_admin/dept_list");

    }

    public function forget_pwd(){

        return  $this->view->fetch("login/forget_pwd");

    }

    public function info(){


        $org_id = Session::get('org_id');
        if(!$org_id){
            $this->redirect('/register');
        }

        $org =  Db::table('organization')->where('id',$org_id)->find();
        $this->assign('org', $org);



//        $item_type = 'qrcode';
//       // $logo = ROOT_PATH."static/css/default/img/logo.png";
//        $data = "test";
//
//        $errorCorrectionLevel = 'H';//容错级别
//        $matrixPointSize = 12;//生成图片大小
//
//
//        $save_dir =  '/picture' ;
//        $upload_path = ROOT_PATH.'uploads'.$save_dir;
//        if (!file_exists($upload_path)){
//            mkdir($upload_path);
//        }
//        $QR =$upload_path.'/'.$org_id.time().rand(1,10000000).".png";//已经生成的原始二维码图
//        //$delete_file = $QR;
//
//        \QRcode::png($data, $QR, $errorCorrectionLevel, $matrixPointSize, 2);
//
//
//        $topic_code =  $QR;
//        echo $topic_code;
//        die(0);

        return  $this->view->fetch("user_admin/info");

    }

    public function download_dept(){

        $file = ROOT_PATH."uploads/excel_template/dept.xlsx";
        if(is_file($file)){
            $length = filesize($file);
            $type = mime_content_type($file);
            $showname =  ltrim(strrchr($file,'/'),'/');
            header("Content-Description: File Transfer");
            header('Content-type: ' . $type);
            header('Content-Length:' . $length);
            if (preg_match('/MSIE/', $_SERVER['HTTP_USER_AGENT'])) { //for IE
                header('Content-Disposition: attachment; filename="' . rawurlencode($showname) . '"');
            } else {
                header('Content-Disposition: attachment; filename="' . $showname . '"');
            }
            readfile($file);
            exit;
        } else {
            exit('文件已被删除！');
        }
    }

    public function download_member(){
        $file = ROOT_PATH."uploads/excel_template/member.xlsx";
        if(is_file($file)){
            $length = filesize($file);
            $type = mime_content_type($file);
            $showname =  ltrim(strrchr($file,'/'),'/');
            header("Content-Description: File Transfer");
            header('Content-type: ' . $type);
            header('Content-Length:' . $length);
            if (preg_match('/MSIE/', $_SERVER['HTTP_USER_AGENT'])) { //for IE
                header('Content-Disposition: attachment; filename="' . rawurlencode($showname) . '"');
            } else {
                header('Content-Disposition: attachment; filename="' . $showname . '"');
            }
            readfile($file);
            exit;
        } else {
            exit('文件已被删除！');
        }
    }

    public function member_list(){

        $org_id = Session::get('org_id');
        if(!$org_id){
            $this->redirect('/register');
        }
        $list =  Db::table('users')->where('org_id',$org_id)->select();
        foreach ($list as $key => $val){
            $dept =  Db::table('dept')->where('id',$val['dept_id'])->find();
            $list[$key]['dept'] = $dept['name'];
        }
        $this->assign('list', $list);
        return  $this->view->fetch("user_admin/member_list");

    }

    public function join_member_list(){

        $org_id = Session::get('org_id');
        if(!$org_id){
            $this->redirect('/register');
        }
        $list =  Db::table('tmp_users')->where('org_id',$org_id)->select();
        $this->assign('list', $list);
        return  $this->view->fetch("user_admin/join_member_list");

    }

    public function  add_join_member(){
        $is_add = 1;
        $this->assign('add',$is_add);

        $org_id = Session::get('org_id');
        $list =  Db::table('dept')->where('org_id',$org_id)->select();
        $this->assign('list', $list);

        return  $this->view->fetch("user_admin/add_join_member");

    }



    public function upload_dept_file(){
        error_log("==========upload_dept_file==============");
        Loader::import('PHPExcel.PHPExcel');
        $org_id = Session::get('org_id');

        $file = request()->file('dept_file');
        if($file){
           // $info = $file->move(ROOT_PATH . 'uploads' . DS . 'excel');
            $info = $file -> validate(['size' => 1024000000]) -> move(ROOT_PATH.'uploads'.DS.'excel');

            if($info){
                // 成功上传后 获取上传信息
                // 输出 jpg
//                echo $info->getExtension();
//                // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                $name =  $info->getSaveName();
                $filename = ROOT_PATH."uploads/excel/".$name;
                $objReader =\PHPExcel_IOFactory::createReader('Excel2007');
                $objPHPExcel = $objReader->load($filename, $encode = 'utf-8');

                $excel_array = $objPHPExcel->getsheet(0)->toArray();   //转换为数组格式
                array_shift($excel_array);  //删除第一个数组(标题);
                $deptList = [];

                error_log("====excel_array=====".json_encode($excel_array));

                foreach($excel_array as $k=>$v) {

                        $deptName = $v[1];
                        if(trim($deptName) != '名称' && trim($deptName) != ''){
                            $deptList[$k]['name'] = $deptName;
                            $deptList[$k]['org_id'] = $org_id;

                        }
                }
                Db::name('dept')->insertAll($deptList); //批量插入数据

            }else{
                // 上传失败获取错误信息
                echo $file->getError();
            }
        }

    }


    public function upload_member_file(){
        Loader::import('PHPExcel.PHPExcel');
        $org_id = Session::get('org_id');

        $file = request()->file('dept_file');
        if($file){
            // $info = $file->move(ROOT_PATH . 'uploads' . DS . 'excel');
            $info = $file -> validate(['size' => 1024000000]) -> move(ROOT_PATH.'uploads'.DS.'excel');

            if($info){
                // 成功上传后 获取上传信息
                // 输出 jpg
//                echo $info->getExtension();
//                // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                $name =  $info->getSaveName();
                $filename = ROOT_PATH."uploads/excel/".$name;
                $objReader =\PHPExcel_IOFactory::createReader('Excel2007');
                $objPHPExcel = $objReader->load($filename, $encode = 'utf-8');

                $excel_array = $objPHPExcel->getsheet(0)->toArray();   //转换为数组格式
                array_shift($excel_array);  //删除第一个数组(标题);
                $member_list = [];

                //error_log("====excel_array=====".json_encode($excel_array));

                foreach($excel_array as $k=>$v) {

                    $name = $v[1];
                    $mail = $v[2];
                    $phone = $v[3];
                    $dept_name = $v[4];

                    if(trim($name) != '姓名' && trim($name) != ''){
                        $tmp =  Db::table('tmp_users')->where('email',$mail)->where('name',$name)->find();
                        if(!$tmp){
                            $member_list[$k]['name'] = $name;
                            $member_list[$k]['email'] = $mail;
                            $member_list[$k]['phone'] = $phone;
                            $member_list[$k]['dept_name'] = $dept_name;
                            $member_list[$k]['org_id'] = $org_id;
                        }



                    }

                    // if(trim($deptName) != '名称' && trim($deptName) != ''){
//                        $deptList[$k]['name'] = $deptName;
//                        $deptList[$k]['org_id'] = $org_id;

                    //}
                }
                Db::name('tmp_users')->insertAll($member_list); //批量插入数据

            }else{
                // 上传失败获取错误信息
                echo $file->getError();
            }
        }

    }

    public function send_mail(){
        Loader::import('PHPMailer.PHPMailer');
        $org_id = Session::get('org_id');
        $org_name = Session::get('org_name');

        $id = $_POST['id'];


        $mail = new PHPMailer();          //实例化PHPMailer对象
        $mail->CharSet = 'UTF-8';           //设定邮件编码，默认ISO-8859-1，如果发中文此项必须设置，否则乱码
        $mail->IsSMTP();                    // 设定使用SMTP服务
        $mail->SMTPDebug = 1;               // SMTP调试功能 0=关闭 1 = 错误和消息 2 = 消息
        $mail->SMTPAuth = true;             // 启用 SMTP 验证功能
        $mail->SMTPSecure = 'ssl';          // 使用安全协议
        $mail->Host = "smtp.126.com"; // SMTP 服务器
        $mail->Port = 465;                  // SMTP服务器的端口号
        $mail->Username = "erims2017@126.com";    // SMTP服务器用户名
        $mail->Password = "erims2017";     // SMTP服务器密码
        $mail->SetFrom('erims2017@126.com', 'Erims');
        $replyEmail = 'erims2017@126.com';                   //留空则为发件人EMAIL
        $replyName = 'Erims';                    //回复名称（留空则为发件人名称）
        $mail->AddReplyTo($replyEmail, $replyName);
        $mail->Subject = $org_name."邀请注册";
        $org =  Db::table('organization')->where('id',$org_id)->find();


        $org_code = 'http://'.$_SERVER['HTTP_HOST']."/uploads/".$org['scan_code'];
        $mail->MsgHTML("欢迎您使用 Erims,请按一下流程加入".$org_name."<br/> 第一步:扫描二维码下载 APP 点击帐号注册<br/><img width='150px' height='150px' src='http://im.50xue.com/uploads/download/download.png '> <br/>
        第二步: 注册以后进入公司选择页面,点击选择公司 扫描二维码 选择你所在的部门<br/> <img width='150px' height='150px' src='$org_code' >");
        if($id == -1){
            $list =  Db::table('tmp_users')->where('org_id',$org_id)->select();
            foreach ($list as $key => $val){

                error_log("======mail=======".$val['email']);
                $mail->AddAddress($val['email'], "test");
            }
            $mail->Send();

        }else{
            $user =  Db::table('tmp_users')->where('id',$id)->find();
            $mail->AddAddress($user['email'], "test");
            $mail->Send();
        }
        //return $mail->Send() ? true : $mail->ErrorInfo;
        echo 200;
    }

    public function meet_list(){
        $org_id = Session::get('org_id');
        if(!$org_id){
            $this->redirect('/register');
        }
        $meet =  Db::table('meet')->where('org_id',$org_id)->select();
        foreach ($meet as $key=> $val){

            $list = Db::table('meet_user')->where('mid',$val['id'])->select();
            $meet[$key]['time']= date('Y-m-d H:i',$val['meet_time']);
            if($list){
                $meet[$key]['num']= count($list);
            }else{
                $meet[$key]['num']= 0;

            }

        }
        $this->assign('meet', $meet);
        return  $this->view->fetch("user_admin/meet_list");

    }

    public function meet_user_list(){

        $id = $_GET['id'];
        $list = Db::table('meet_user')->where('mid',$id)->select();
        foreach ($list as $key=> $val){

            $user = Db::table('users')->where('connect_uid',$val['uid'])->find();
            if($user){
                $list[$key]['name'] = $user['nickname'];
            }
            if($val['is_creator'] == 1){
                $list[$key]['is_creator'] = '发起人';
            }else{
                $list[$key]['is_creator'] = '参与者';
            }
            $join_user = Db::table('meet_user_record')->where('uid',$val['uid'])->where('mid',$val['mid'])->find();
            if($join_user){

                if($join_user['type'] == 1){
                    $list[$key]['status'] = '请假';
                    $list[$key]['mark'] = $join_user['mark'];

                }else{
                    $list[$key]['status'] = '签到';
                    $list[$key]['mark'] = '';

                }
                $list[$key]['time'] =    date('Y-m-d H:i',$join_user['add_time']);

            }else{
                $list[$key]['status']  ='';
                $list[$key]['time']  ='';
                $list[$key]['mark'] = '';

            }
        }
        $this->assign('list', $list);

        return  $this->view->fetch("user_admin/meet_user_list");

    }

    public function push(){
        $org_id = Session::get('org_id');
        if(!$org_id){
            $this->redirect('/register');
        }
        $this->assign('org_id', $org_id);
        return  $this->view->fetch("user_admin/push");

    }

    public function push_msg(){


        $msg = $_POST['msg'];
        $id = $_POST['id'];


        $appSecret = '2hVZ4EhoXdep'; // 开发者平台分配的 App Secret。
        $nonce = rand(); // 获取随机数。
        $timestamp = time(); // 获取时间戳。

        $signature = sha1($appSecret.$nonce.$timestamp);

        $url = "https://api.cn.ronghub.com/push.json";
        //$data = {"platform":["ios","android"],"audience":{"userid":["123","456"]},"notification":{"alert":"this is a push"}}
        $data = array();
        $platform_data = array();
        $platform_data[0] = "ios";
        $platform_data[1] = "android";
        $audience_data = array();
        $user_data = array();
        $users = Db::table('users')->where('org_id',$id)->select();
        foreach ($users as $key => $val){
            $user_data[$key] = $val['connect_uid'];
        }
        $content_data = array();
        $content_data['type'] = "system";
        $content_data['content'] = $msg;

        $audience_data['userid'] = $user_data;
        $notification_data = array();
        $notification_data['alert'] = json_encode($content_data);
        $data['platform'] = $platform_data;
        $data['audience'] = $audience_data;
        $data['notification'] = $notification_data;
//
//        echo json_encode($data);
//        die(0);

        $result = $this->http_post_json($url,json_encode($data));
        echo 200;
    }
    function http_post_json($url, $jsonStr)
    {

        $nonce = mt_rand();
        $timeStamp = time();
        $sign = sha1('2hVZ4EhoXdep'.$nonce.$timeStamp);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: Application/json',
                'Content-Length: ' . strlen($jsonStr),
                'RC-Nonce:'.$nonce,
                'RC-Timestamp:'.$timeStamp,
                'RC-Signature:'.$sign,
                'RC-App-Key:'.'vnroth0kv4oco',
            )
        );
        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        return array($httpCode, $response);
    }

}
